package com.peaksport.framework.metadata.sorter;

import java.io.Serializable;

import com.peaksport.framework.metadata.venum.SortDirectionEnum;
import com.peaksport.framework.util.PKPublicUtils;

/**
 * 
 * 排序属性
 *
 * @author jinbin_guo
 *
 */
@SuppressWarnings("serial")
public class SorterItemInfo implements Serializable {

    /** 实体排序属性 */
    private String properyName;
    /** 排序方向 */
    private SortDirectionEnum direction;

    /** 排序属性对应的数据库字段,含解析后的表别名*/
    private String dbField;

    public SorterItemInfo() {
        this(null,null);
    }

    public SorterItemInfo(String properyName) {
        this(properyName,SortDirectionEnum.ASC);
    }

    public SorterItemInfo(String properyName, SortDirectionEnum direction) {
        this.properyName = properyName;
        this.direction = direction;
    }

    public String getProperyName() {
        return properyName;
    }

    public void setProperyName(String properyName) {
        this.properyName = properyName;
    }

    public String getDbField() {
        return dbField;
    }

    public void setDbField(String dbField) {
        this.dbField = dbField;
    }

    public SortDirectionEnum getDirection() {
        return direction;
    }

    public void setDirection(SortDirectionEnum direction) {
        this.direction = direction;
    }

    public String toSQL() {
        StringBuilder sql = new StringBuilder();
        sql.append(dbField);
        if (PKPublicUtils.equals(SortDirectionEnum.DESC, direction))
            sql.append(PKPublicUtils.SPACE).append(direction.getValue());
        return sql.toString();
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();
        str.append("{properyName:\"").append(properyName).append("\",")
                .append("direction=\"").append(direction.getValue()).append("\",")
                .append("dbField=\"").append(dbField).append("\"}");
        return str.toString();
    }
}
